package com.ruijixiang.leetcode.everyday.leetcode2024.leetcode202409;

import java.util.*;

public class leetcode20240925 {
    /**
     * 使用笛卡尔积来计算
     * @param ideas
     * @return
     */
    public long distinctNames(String[] ideas) {
        long res=0;
        List<Character> list=new ArrayList<>();
        Map<Character, Set<String>> map=new HashMap<>();

        for(String idea : ideas){
            if(!map.containsKey(idea.charAt(0))){
                list.add(idea.charAt(0));
                map.put(idea.charAt(0),new HashSet<>());
            }
            map.get(idea.charAt(0)).add(idea.substring(1));
        }

        for(int i=0;i<list.size();i++){
            for(int j=i+1;j<list.size();j++){
                Set<String> set1=map.get(list.get(i));
                Set<String> set2=map.get(list.get(j));
                Set<String> inter=new HashSet<>(set1);

                inter.retainAll(set2);// 获取两个集合的交集，返回给inter

                res+=(set1.size()-inter.size())*(set2.size()-inter.size())*2L;// 位置可以互换，所以需要*2
            }
        }
        return res;
    }
}
